<?php
class Calendar {
	var $YEAR, $MONTH, $DAY;
	var $COLOR_TODAY = "lightgray"; //当前日期的背景色
	var $COLOR_THIS_DAY = "lightOrg"; //当前月的背景色

	var $WEEK = array ("日","一","二","三","四","五","六");
	var $_MONTH = array (
		"1" => "一月",
		"2" => "二月",
		"3" => "三月",
		"4" => "四月",
		"5" => "五月",
		"6" => "六月",
		"7" => "七月",
		"8" => "八月",
		"9" => "九月",
		"10" => "十月",
		"11" => "十一月",
		"12" => "十二月"
	);

	function setYear($year) {
		$this->YEAR = $year;
	}
	//获得年份

	function getYear() {
		return $this->YEAR;
	}
	//设置月份
	function setMonth($month) {
		$this->MONTH = $month;
	}
	//获得月份
	function getMonth() {
		return $this->MONTH;
	}
	//设置日期
	function setDay($day) {
		$this->DAY = $day;
	}
	//获得日期
	function getDay() {
		return $this->DAY;
	}
	//获得方法内指定的日期的星期数 0（表示星期天）到 6（表示星期六）

	function getWeek($year, $month, $day) {
		$week = date("w", mktime(0, 0, 0, $month, $day, $year)); //获得星期
		return $week; //获得星期
	}

	//为$YEAR,$MONTH,$DAY赋值
	function _env() {
		if (isset ($_POST['month'])) { //有指定月
			$month = $_POST['month'];
		} else {
			$month = date("n"); //默认为本月
		}

		if (isset ($_POST['year'])) { //有指年
			$year = $_POST['year'];
		} else {
			$year = date("Y"); //默认为本年
		}
		$this->setYear($year);
		$this->setMonth($month);
		$this->setDay(date("d"));
	}

	public function returnCalendar(){
		$str = $this->out($this->YEAR, $this->MONTH, $this->DAY);
		return $str;
	}
	//打印日历

	public function out($year , $month , $day) {	
		$has = false;	
		$week = $this->getWeek($year, $month, $day); //获得日期为星期几 （例如今天为2011-10-31，星期一）
		$fweek = $this->getWeek($year, $month, 1); //获得此月第一天为星期几

		$str = '<div class="week">';
		for ($i = 0; $i < count($this->WEEK); $i++) { //打印星期标头
			if ($i==0 || $i==6) {
				$str .= '<li class="active">' . $this->WEEK[$i] . '</li>';
			}else{
				$str .= '<li>' . $this->WEEK[$i] . '</li>';
			}			
		}
		$str .= '</div>';

		$str .= '<div class="dayList">';
		//打印开头
		$totalTD = date("t", mktime(0, 0, 0, $month, $day, $year))+$fweek;
		for ($Tmpc = 0; $Tmpc < $fweek; $Tmpc++) {
			$str .= "<li></li>";
		}

		for ($Tmpb = 1; $Tmpb <= date("t", mktime(0, 0, 0, $month, $day, $year)); $Tmpb++) { //打印所有日期

			$Tmpb = sprintf("%02d", $Tmpb);

			if ((strcmp($Tmpb, $day) == 0) && ($month == date("m")) && ($year == date("Y"))) { //获得当前日期，做标记
				$flag = 'class="local"';
			}else{
				$flag = '';
			}

			$temp = $year . '-' . $month . '-' . $Tmpb;
			if (in_array($temp, $this->jieri)) {
				$has=true;
				$index = $this->findId($this->jieri,$temp);	
				$str .= '<li class="active">'.$Tmpb.'<i>￥'.$this->money[$index].'</i>';
				if ($this->flag[$index]) {
					$str .= '<span>特</span>';
				}
				$str .= '</li>';
			}else{
				$str .= '<li '.$flag.'>'.$Tmpb.'</li>';
			}
		}

		//打印尾
		

		$eweek = $this->getWeek($year, $month, ($Tmpb-1)); //获得此月第一天为星期几

		for ($Tmpc = 6; $Tmpc > $eweek; $Tmpc--) {
			$str .= "<li></li>";
		}

		$str .= '</div>';

		return $str;

		
	} //out();

	public function findId($arr,$id){
        foreach ($arr as $key => $value) {
        	if ($id==$value) {
        		return $key;
        	}
        }
        return FALSE;
    }

} //class Calendar
?>
